/**
 * Created by shandianhaizan on 16/6/30.
 */
define(['validator'], function (validator) {

    function _controller($scope, $routeParams, $location, $timeout, httpService) {

        var id = $routeParams.id;
        var model ="Customer";
        $scope.properties = [];

        $scope.remove = function(one, arr){
            arr.splice( $.inArray(one, arr), 1);
        };
        var isUpdate = function(){
            return id && id!=="new"
        };


        $scope.validation = function(arg_customer){
          if(arg_customer.mobilePhoneNumber && !validator.isMobilePhone(arg_customer.mobilePhoneNumber, "zh-CN")){
              return {success:false, msg:"手机号格式错误"};
          }
          if(arg_customer.email && !validator.isEmail(arg_customer.email) ){
              return {success:false, msg:"邮箱格式错误"};
          }
          return {success:true}

        };
        $scope.save = function () {
            $scope.$broadcast('show-errors-check-validity');
            var validation = $scope.validation($scope.customer);
            if(!validation.success) return $scope.msg.show(validation.msg);
            httpService.saveUser($scope.customer).then(function (data) {
                $scope.msg.show(data);
                if($scope.isHandleSuccess(data) && !isUpdate()){
                    $location.path('/edit-customer/'+data.data.objectId);
                }else if(data.code && data.code == 202){
                    $scope.msg.show("用户名已被注册");
                }
            });

        };

        $scope.initGroup = function () {
            httpService.get("Group", {all:true}).then(function (data) {
                $scope.groups = data.data.data;
            });
        };


        var getPointGroup = function(rules, cp){
            var leftCp = function(cp,  rule){
                if(!rule) return false;
                if(!rule.leftOperator || !rule.leftNum) return true;
                if(rule.leftOperator == ">" && cp > rule.leftNum) return true;
                if(rule.leftOperator == ">=" && cp >= rule.leftNum) return true;
                return false;
            };
            var rightCp = function(cp, rule){
                if(!rule) return false;
                if(!rule.rightOperator || !rule.rightNum) return true;
                if(rule.rightOperator == "<" && cp < rule.rightNum) return true;
                if(rule.rightOperator == "<=" && cp <= rule.rightNum) return true;
                return false;
            };

            for(var i = 0, l= rules.length; i<l; i++){
                var rule = rules[i];
                if(leftCp(cp, rule) && rightCp(cp, rule)){
                    return rule.groupName || "";
                }
            }
        };


        $scope.initPointRule = function () {
            httpService.get("PointRule", {all:true}).then(function (data) {
                $scope.pointRules = data.data.data;
                $scope.groupName = getPointGroup($scope.pointRules, $scope.customer.orderAmount);
                console.log($scope.groupName);
            });
        };


        $scope.initGroup();



        $scope.reset = function () {
            $scope.$broadcast('show-errors-reset');
            $scope.customer = {};
        };


        if(isUpdate()){
            $scope.editType = "编辑";
            httpService.get(model, id).then(function (data) {
                $scope.customer = data.data;
                $scope.initPointRule();
            });
        }else{
            $scope.editType = "添加";
            $scope.customer = {
            };
        }




    }

    return _controller;
});

